package com.aikon.wht.poi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestOfPoi {
	
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String args[]) throws IOException {
		Workbook wb = new XSSFWorkbook();
		FileOutputStream fileOut = new FileOutputStream(new File("D:\\workbook.xlsx"));

		// 创建名为testOfPoi的sheet.
		Sheet sheet = wb.createSheet("testOfPoi");

		// 创建2行B列单元格.
		Row row1 = sheet.createRow(1);// 0开始.
		Cell cell11 = row1.createCell(1);// 0开始.

		// 单元格内容.
		cell11.setCellValue("This Is The Title");

		// 单元格合并.
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 8));// 行始, 行末,列始,列末
		// 0开始.

		Font font = wb.createFont();
		CellStyle style = wb.createCellStyle();

		// 字号.字体.斜体.删除线.字颜色.
		font.setFontHeightInPoints((short) 24);
		font.setFontName("Courier New");
		font.setItalic(true);
		font.setStrikeout(true);
		font.setColor((short) 2);

		// 填充.
		style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
		style.setFillPattern(CellStyle.ALIGN_CENTER);

		// 对齐方式.
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.ALIGN_CENTER);

		style.setFont(font);
		cell11.setCellStyle(style);

		Row row3 = sheet.createRow((short) 3);
		Cell cell39 = row3.createCell(9);
		cell39.setCellValue(Calendar.getInstance());
		// 列尺寸.
		sheet.autoSizeColumn((short) 9);
		// 时间格式化.
		CreationHelper createHelper = wb.getCreationHelper();
		style = wb.createCellStyle();
		style.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
		cell39.setCellStyle(style);

		for (int i = 6; i < 41; i++) {
			Row row = sheet.createRow((short) i);
			for (int j = 2; j < 8; j++) {
				Cell cell = row.createCell(j);
				cell.setCellValue(i + "" + j);
				style = wb.createCellStyle();

				// 边框.
				style.setBorderBottom(CellStyle.BORDER_THIN);
				style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
				style.setBorderLeft(CellStyle.BORDER_THIN);
				style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
				style.setBorderRight(CellStyle.BORDER_THIN);
				style.setRightBorderColor(IndexedColors.BLACK.getIndex());
				style.setBorderTop(CellStyle.BORDER_THIN);
				style.setTopBorderColor(IndexedColors.BLACK.getIndex());
				cell.setCellStyle(style);
			}
		}

		// 打印区域.
		wb.setPrintArea(0, "$A$1:$K$42");
		wb.write(fileOut);

		fileOut.close();

	}

}
